import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Game extends JPanel {
	private Main window;
	private Menu menu;
	private Image backGround;
	private Miner miner;
	private Image mImage;
	private int goldNumber;
	private int levelIndex;
	private Gold[] golds;

	public Game(Main w, Menu m) {
		menu = m;
		window = w;
		backGround = (new ImageIcon(getClass().getResource(
				"image/backGrounds/gameBackGround.jpg"))).getImage();
		miner = new Miner();
		levelIndex = 1;
		golds = new Gold[50];

		window.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int key = e.getKeyCode();
				if (key == KeyEvent.VK_ESCAPE) {
					window.setContentPane(menu);
					window.setVisible(true);
				}
				if (key == KeyEvent.VK_LEFT) {
					if (miner.getPosX() > 3) {
						miner.setPosX(miner.getPosX() - 3);
						repaint();
					}
				}
				if (key == KeyEvent.VK_RIGHT) {
					if (miner.getPosX() < 702) {
						miner.setPosX(miner.getPosX() + 3);
						repaint();
					}
				}

			}

		});
		this.readLevelDates();
	}

	public void update(Graphics g) {
		paint(g);
	}

	public void paint(Graphics g) {
		Dimension d = getSize();
		checkOffscreenImage();
		Graphics offG = mImage.getGraphics();
		offG.setColor(Color.red);
		offG.fillRect(0, 0, d.width, d.height);
		paintOffscreen(mImage.getGraphics());
		g.drawImage(mImage, 0, 0, null);

	}

	private void checkOffscreenImage() {
		Dimension d = getSize();
		if (mImage == null || mImage.getWidth(null) != d.width
				|| mImage.getHeight(null) != d.height) {
			mImage = createImage(d.width, d.height);
		}
	}

	public void paintOffscreen(Graphics g) {
		g.drawImage(backGround, 0, 0, 800, 600, this);
		g.drawImage(miner.getMinerImage(), miner.getPosX(), miner.getPosY(),
				miner.getMinerWidth(), miner.getMinerHeight(), this);
		goldRocksPaint(g);
	}

	public void readLevelDates() {
		String str;
		int tempX;
		int tempY;
		int tempMass;
		int tempPrice;
		
		try {
			BufferedReader reader = new BufferedReader(new FileReader(
					"bin/levels/level"+levelIndex+ ".txt"));
			str = reader.readLine();
			goldNumber = Integer.parseInt(str); 
			for(int i = 0; i<goldNumber; i++){
				str = reader.readLine();
				tempX = Integer.parseInt(str);
				str = reader.readLine();
				tempY = Integer.parseInt(str);
				str = reader.readLine();
				tempMass = Integer.parseInt(str);
				str = reader.readLine();
				tempPrice = Integer.parseInt(str);
				golds[i] = new Gold(tempX,tempY,tempMass,tempPrice);
			}
			
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	public void goldRocksPaint(Graphics g){
		for(int i = 0; i<goldNumber; i++ ){
			if( golds[i].isCollected() == false ){
				g.drawImage(golds[i].getGoldImage(), golds[i].getPosX(), golds[i].getPosY(),
						(golds[i].getGoldImage()).getWidth(null), (golds[i].getGoldImage()).getHeight(null), this);
			}
		}
	}
}


